I found a problem with the previous code: I added info on butterfly_flight to the data before calculating the distances and that makes the paths larger than they actually are. This version does not have that problem.
I also changed the code in order to use purrr, a package for doing functional programming. Basically, it makes it way easier to maintain the code by making it shorter.
I suppose that’s enough rambling from me, let’s give you the tables, that’s what you want after all 😄
list_of_packages <- c("tidyverse", "here", "fs", "plotly", "DT", "signal")
new_packages <-
list_of_packages[!(list_of_packages %in% installed.packages()[, "Package"])]
if (length(new_packages))
install.packages(new_packages)
library(tidyverse)
library(here)
library(fs)
library(plotly)
library(DT)
library(signal)
fs::dir_ls(here::here('wand'), recurse = T, regexp = "unpaired-points-xyz.csv$") %>%
purrr::map_dfr(readr::read_csv, .id = "source") -> all_flights
all_flights %>%
tidyr::drop_na() -> all_flights
calc_flight_dist <- function(dat) {
dat_dist <- {{dat}} %>%
dist() %>% # calculates distance
as.matrix() # converts it to a matrix
return(sum((dat_dist[row(dat_dist) == col(dat_dist) + 1]))) # sums the distance between points by summing the points in line n and column n + 1
}
plots_flight_path_raw <- function(dat) {
plotly::plot_ly(data = dat,
type = "scatter3d",
x = dat$x_1,
y = dat$y_1,
z = dat$z_1,
mode = "lines")
}
plots_flight_path_butterworth <- function(dat) {
plotly::plot_ly(data = dat,
type = "scatter3d",
x = dat$x_butterworth,
y = dat$y_butterworth,
z = dat$z_butterworth,
mode = "lines")
}
calc_straight_dist <- function(dat) {
dist_dat <-
dat %>%
dist() %>%
as.matrix()
return(dist_dat[1, nrow(dist_dat)])
}
# creating butterworth filter
butterworth_filter <-
signal::butter(n = 4, # order
W = 0.5, # critical frequency
type = "low")
all_flights %>%
dplyr::mutate(
x_butterworth = signal::filter(butterworth_filter, x_1),
y_butterworth = signal::filter(butterworth_filter, y_1),
z_butterworth = signal::filter(butterworth_filter, z_1)
) %>%
dplyr::group_by(source) %>%
tidyr::nest(data_raw = ends_with("_1"),
data_butterworth = ends_with("_butterworth")) %>%
dplyr::mutate(
butterfly = stringr::str_extract(source, pattern = "b\\d{3}"),
take = stringr::str_extract(source, pattern = "t\\d{1}"),
distance_raw = purrr::map_dbl(data_raw, ~ calc_flight_dist(.x)),
straight_distance_raw = purrr::map_dbl(data_raw, ~ calc_straight_dist(.x)),
sinuosity_raw = distance_raw / straight_distance_raw,
plot_3d_raw = purrr::map(data_raw, ~ plots_flight_path_raw(.x)),
distance_butterworth = purrr::map_dbl(data_butterworth, ~ calc_flight_dist(.x)),
straight_distance_butterworth = purrr::map_dbl(data_butterworth, ~ calc_straight_dist(.x)),
sinuosity_butterworth = distance_butterworth / straight_distance_butterworth,
plot_3d_butterworth = purrr::map(data_butterworth, ~ plots_flight_path_butterworth(.x))
) %>%
dplyr::ungroup() -> processed
processed %>%
dplyr::select(butterfly, take, everything(), -source, -data_raw,, data_butterworth, -plot_3d_raw, -plot_3d_butterworth) %>%
DT::datatable(extensions = 'Buttons',
options = list(dom = 'Blfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
lengthMenu = list(c(10,25,50,-1),
c(10,25,50,"All"))))
# all_flights
fs::dir_ls(here::here('wand'), recurse = T, regexp = "unpaired-points-xyz.csv$") %>%
purrr::map_dfr(readr::read_csv, .id = "source") %>%
dplyr::group_by(source) %>%
dplyr::mutate(row_n = row_number()) %>%
dplyr::ungroup() %>%
tidyr::drop_na() %>%
dplyr::group_by(source) %>%
dplyr::summarise(flight_duration_frames = max(row_n) - min(row_n))
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
##
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## x_1 = col_double(),
## y_1 = col_double(),
## z_1 = col_double()
## )
## # A tibble: 35 x 2
## source flight_duration_fra…
## <chr> <int>
## 1 /home/francisko/coding/r/flight-path-experiment/wand/b1… 177
## 2 /home/francisko/coding/r/flight-path-experiment/wand/b1… 101
## 3 /home/francisko/coding/r/flight-path-experiment/wand/b1… 152
## 4 /home/francisko/coding/r/flight-path-experiment/wand/b1… 156
## 5 /home/francisko/coding/r/flight-path-experiment/wand/b1… 185
## 6 /home/francisko/coding/r/flight-path-experiment/wand/b2… 198
## 7 /home/francisko/coding/r/flight-path-experiment/wand/b2… 211
## 8 /home/francisko/coding/r/flight-path-experiment/wand/b2… 151
## 9 /home/francisko/coding/r/flight-path-experiment/wand/b2… 147
## 10 /home/francisko/coding/r/flight-path-experiment/wand/b2… 89
## # … with 25 more rows
butterworth_filter <-
signal::butter(n = 4, # order
W = 0.5, # critical frequency
type = "low")